package com.mysiteforme.admin.base;

import com.alibaba.fastjson.JSON;
import com.mysiteforme.admin.realm.AuthRealm.ShiroUser;
import org.apache.shiro.SecurityUtils;

/**
 * Created by wangl on 2017/11/25.
 * todo:
 */
public class MySysUser {
    /**
     * 取出Shiro中的当前用户LoginName.
     */
    public static String icon() {
        return ShiroUser().getIcon();
    }

    public static Long id() {
        return ShiroUser().getId();
    }

    public static String loginName() {
        return ShiroUser().getloginName();
    }

    public static String nickName(){
        return ShiroUser().getNickName();
    }

    public static ShiroUser ShiroUser() {
        Object obj = SecurityUtils.getSubject().getPrincipal();
        ShiroUser shiroUser= null;
        if (obj!=null) {
            shiroUser = obj instanceof ShiroUser ? (ShiroUser) obj : JSON.parseObject(JSON.toJSON(obj).toString(), ShiroUser.class);
            return shiroUser;
        }
        else{
            return shiroUser;
        }//兼容spring-devtools热部署导致同类型转换异常，下面两种装换方式其实是一样的
//        if(obj instanceof ShiroUser) {
//            user = (ShiroUser) obj;
//        }else{
//            user = JSON.parseObject(JSON.toJSON(obj).toString(),ShiroUser.class);
//        }


    }
}
